package com.study.mvcconfig.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

public class DateUtilsJava8 {
	
	private final static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
	private final static DateTimeFormatter shortDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
	private final static DateTimeFormatter shortDtf1 = DateTimeFormatter.ofPattern("yyyyMMdd");
	private final static DateTimeFormatter longDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
	private final static DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern("HH:mm:ss");
	private final static DateTimeFormatter timeDtf1 = DateTimeFormatter.ofPattern("HHmmss");
	
	
	public static void main(String[] args) {
		System.out.println(getDateTimeToString(LocalDateTime.now()));
		System.out.println(getStringToDateTime("20170705165439"));
		System.out.println(getStringToDateTime1("2017-07-05 16:54:39"));
		
		System.out.println(getStringToLocalDate("2017-07-05"));
		System.out.println(getStringToLocalDate1("20170705"));
		System.out.println(getLocalDateToString(LocalDate.now()));
		System.out.println(getLocalDateToString(LocalDate.now()));
		
		System.out.println(getTimeToString(LocalTime.now()));
		System.out.println(getTimeToString1(LocalTime.now()));
		System.out.println(getStringToTime("16:54:39"));
		System.out.println(getStringToTime1("165439"));
		
		LocalDateTime ldt = LocalDateTime.now();
		System.out.println(ldt);
		System.out.println(ldt.format(DateTimeFormatter.ISO_DATE));//转日期
		System.out.println(ldt.format(DateTimeFormatter.ISO_TIME));//转时间
		System.out.println(ldt.getDayOfMonth());
		System.out.println(ldt.getDayOfYear());
		System.out.println(ldt.getDayOfWeek());
		System.out.println(ldt.getYear());
		System.out.println(ldt.getMonthValue());
		System.out.println(ldt.getHour());
		System.out.println(ldt.getMinute());
		System.out.println(ldt.getSecond());
		System.out.println(getDateToSixString(LocalDate.now()));
	}
	
	/**
	 * 将日期转为yyMMdd六位字符串
	 * @param date
	 * @return
	 */
	public static String getDateToSixString(LocalDate date){
		String result = getLocalDateToString1(date);
		return result.substring(2);
	}
	
	/**
	 * 日期时间转为无间隔字符串
	 * @param date
	 * @return
	 */
	public static String getDateTimeToString(LocalDateTime date){
		return date.format(dtf);
	}
	
	/**
	 * yyyyMMddHHmmss 的字符串转为日期时间类
	 * @param date
	 * @return
	 */
	public static LocalDateTime getStringToDateTime(String date){
		return LocalDateTime.parse(date, dtf);
	}
	
	/**
	 * yyyy-MM-dd HH:mm:ss 的字符串转为日期时间类
	 * @param date
	 * @return
	 */
	public static LocalDateTime getStringToDateTime1(String date){
		return LocalDateTime.parse(date, longDtf);
	}
	
	/**
	 * yyyy-MM-dd 的字符串转为日期
	 * @param date
	 * @return
	 */
	public static LocalDate getStringToLocalDate(String date){
		return LocalDate.parse(date,shortDtf);
	}
	
	/**
	 * 日期转为 yyyy-MM-dd 字符串
	 * @param date
	 * @return
	 */
	public static String getLocalDateToString(LocalDate date){
		return date.format(shortDtf);
	}
	
	/**
	 * yyyyMMdd 的字符串转日期
	 * @param date
	 * @return
	 */
	public static LocalDate getStringToLocalDate1(String date){
		return LocalDate.parse(date,shortDtf1);
	}
	
	/**
	 * 日期转为 yyyyMMdd 字符串
	 * @param date
	 * @return
	 */
	public static String getLocalDateToString1(LocalDate date){
		return date.format(shortDtf1);
	}
	
	/**
	 * 时间转为 HH:mm:ss 字符串
	 * @param date
	 * @return
	 */
	public static LocalTime getStringToTime(String date){
		return LocalTime.parse(date, timeDtf);
	}
	
	/**
	 * 时间转为 HHmmss 字符串
	 * @param date
	 * @return
	 */
	public static LocalTime getStringToTime1(String date){
		return LocalTime.parse(date, timeDtf1);
	}
	
	/**
	 * HH:mm:ss 的字符串转为时间
	 * @param date
	 * @return
	 */
	public static String getTimeToString(LocalTime date){
		return date.format(timeDtf);
	}
	
	/**
	 * HHmmss 字符串转为时间
	 * @param date
	 * @return
	 */
	public static String getTimeToString1(LocalTime date){
		return date.format(timeDtf1);
	}
}
